package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.ServiceDescription;
import com.xq.shop.dao.mapper.ServiceDescriptionMapper;
import com.xq.shop.dto.EditServiceDescriptionDto;
import com.xq.shop.service.IServiceDescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.ServiceDescriptionVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务说明表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-19
 */
@Service
public class ServiceDescriptionServiceImpl extends ServiceImpl<ServiceDescriptionMapper, ServiceDescription> implements IServiceDescriptionService {

    @Resource
    private Redis redis;
    @Override
    public List<ServiceDescriptionVo> queryServiceDescription() {

        LambdaQueryWrapper<ServiceDescription> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ServiceDescription::getStatus, DataStatus.NORMAL.getCode());
        List<ServiceDescription> serviceDescriptionList = list(wrapper);
        return beanToVo(serviceDescriptionList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editServiceDescription(EditServiceDescriptionDto dto, Integer userId) {
        ServiceDescription serviceDescription = getById(dto.getId());
        if (Objects.nonNull(serviceDescription)) {
            serviceDescription.setContent(dto.getContent());
            serviceDescription.setUpdateUser(userId);
            updateById(serviceDescription);
        }
        redis.remove("serviceDescription");
    }

    private List<ServiceDescriptionVo> beanToVo(List<ServiceDescription> serviceDescriptionList) {
        List<ServiceDescriptionVo> voList = new ArrayList<>();
        serviceDescriptionList.forEach(sd -> {
            ServiceDescriptionVo vo = new ServiceDescriptionVo();
            BeanUtils.copyProperties(sd, vo);
            voList.add(vo);
        });
        return voList;
    }
}
